package com.wzh.musichome.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.musichome.entity.User;
import com.wzh.musichome.entity.dto.LoginDTO;
import com.wzh.musichome.dao.UserDao;
import com.wzh.musichome.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Past;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzh
 * @since 2020-11-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private JwtUtil JwtUtil;

    @Override
    public Object login(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();

        System.out.println(username);
        System.out.println(password);
        //检测用户名和密码是否为空
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USERNAME_PASSWORD_EMPTY, "用户名/密码不能为空");
        }

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userDao.selectOne(wrapper);
        if (user == null){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USER_NULL, "用户不存在");
        }
        if (!password.matches(user.getPassword())){
            System.out.println(user.getPassword());
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_PASSWORD_WRONG, "密码错误");
        }

        //生成token
        String jwt = JwtUtil.createJWT(user.getUserId()+"",user.getUsername(),true);

        Map<String, String> token = new HashMap<>();
        token.put("token",jwt);
        ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
        data.setData(token);
        data.setMsg("token生成完成");
        return data ;
    }

    @Override
    public Object getUserInfo(String token) {

        //解析令牌
        Claims claims = JwtUtil.paresJWT(token);
        if (claims == null || StringUtils.isBlank(claims.getSubject())){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_TOKEN_FAILURE, "获取token失败");
        }
        String username = claims.getSubject();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userDao.selectOne(wrapper);
        if (user == null){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USER_NULL, "用户不存在");
        }
        user.setPassword(null);
        ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
        data.setData(user);
        data.setMsg("token解析成功");
        return data ;

    }
}
